home *** CD-ROM | disk | FTP | other *** search
/ Skunkware 5 / Skunkware 5.iso / src / X11 / Fresco / build / Unix / config / util / checktree.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-07-12  |  7.5 KB  |  344 lines

  1. /* $XConsortium: checktree.c,v 1.7 94/04/17 20:10:40 gildea Exp $ */
  2.  
  3. /*
  4.  
  5. Copyright (c) 1993  X Consortium
  6.  
  7. Permission is hereby granted, free of charge, to any person obtaining a copy
  8. of this software and associated documentation files (the "Software"), to deal
  9. in the Software without restriction, including without limitation the rights
  10. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. copies of the Software, and to permit persons to whom the Software is
  12. furnished to do so, subject to the following conditions:
  13.  
  14. The above copyright notice and this permission notice shall be included in
  15. all copies or substantial portions of the Software.
  16.  
  17. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
  20. X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
  21. AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  22. CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  23.  
  24. Except as contained in this notice, the name of the X Consortium shall not be
  25. used in advertising or otherwise to promote the sale, use or other dealings
  26. in this Software without prior written authorization from the X Consortium.
  27. */
  28.  
  29. #include <X11/Xos.h>
  30. #include <stdio.h>
  31. #include <sys/stat.h>
  32. #include <sys/param.h>
  33. #include <errno.h>
  34.  
  35. #ifndef X_NOT_POSIX
  36. #include <dirent.h>
  37. #else
  38. #ifdef SYSV
  39. #include <dirent.h>
  40. #else
  41. #ifdef USG
  42. #include <dirent.h>
  43. #else
  44. #include <sys/dir.h>
  45. #ifndef dirent
  46. #define dirent direct
  47. #endif
  48. #endif
  49. #endif
  50. #endif
  51.  
  52. #ifdef S_IFLNK
  53. #define Stat lstat
  54. #else
  55. #define Stat stat
  56. #endif
  57.  
  58. #define CHARSALLOWED \
  59. "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-_."
  60.  
  61. #define fmode_bits_minset 0444
  62. #define fmode_bits_maxset 0777
  63. #define fmode_bits_write  0222
  64. #define dmode_bits_minset 0775
  65.  
  66. #ifdef X_NOT_STDC_ENV
  67. extern int errno;
  68. #endif
  69.  
  70. int dorcs = 1;            /* check RCS file */
  71. int do83 = 1;            /* check for 8+3 clash */
  72. int doro = 1;            /* disallow writable (checked out) files */
  73. int dodot = 1;            /* disallow .files */
  74. int dotwiddle = 1;        /* disallow file~ */
  75.  
  76. int dontcare(fn)
  77.     char *fn;
  78. {
  79.     char *cp;
  80.  
  81.     if (fn[strlen(fn) - 1] == '~')
  82.     return 1;
  83.     cp = strrchr(fn, '.');
  84.     return cp && (!strcmp(cp + 1, "Z") || !strcmp(cp + 1, "PS"));
  85. }
  86.  
  87. checkfile(fullname, fn, fs)
  88.     char *fullname, *fn;
  89.     struct stat *fs;
  90. {
  91.     char *cp;
  92.     int maxlen = 12;
  93.     int len, mode;
  94.  
  95.     if (dodot && fn[0] == '.') {
  96.     printf("dot file: %s\n", fullname);
  97.     return;
  98.     }
  99.     for (len = 0, cp = fn; *cp; len++, cp++) {
  100.     if (!strchr(CHARSALLOWED, *cp)) {
  101.         if (dotwiddle || *cp != '~' || cp[1])
  102.         printf ("bad character: %s\n", fullname);
  103.         break;
  104.     }
  105.     }
  106.     if (len > maxlen && !dontcare(fn))
  107.     printf("too long (%d): %s\n", len, fullname);
  108. #ifdef S_IFLNK
  109.     if ((fs->st_mode & S_IFLNK) == S_IFLNK) {
  110.     printf("symbolic link: %s\n", fullname);
  111.     return;
  112.     }
  113. #endif
  114.     mode = fs->st_mode & (~S_IFMT);
  115.     if ((fs->st_mode & S_IFDIR) == S_IFDIR) {
  116.     maxlen = 14;
  117.     if ((mode & dmode_bits_minset) != dmode_bits_minset)
  118.         printf("directory mode 0%o not minimum 0%o: %s\n",
  119.            mode, dmode_bits_minset, fullname);
  120.     } else if ((fs->st_mode & S_IFREG) != S_IFREG)
  121.     printf("not a regular file: %s\n", fullname);
  122.     else {
  123.     if ((mode & fmode_bits_minset) != fmode_bits_minset)
  124.         printf("file mode 0%o not minimum 0%o: %s\n",
  125.            fs->st_mode, fmode_bits_minset, fullname);
  126.     if (fs->st_nlink != 1)
  127.         printf("%d links instead of 1: %s\n", fs->st_nlink, fullname);
  128.     if (doro && (mode & fmode_bits_write) && !dontcare(fn))
  129.         printf("writable: %s\n", fullname);
  130.     }
  131.     if ((mode & ~fmode_bits_maxset) != 0)
  132.     printf("mode 0%o outside maximum set 0%o: %s\n",
  133.            mode, fmode_bits_maxset, fullname);
  134. }
  135.  
  136. void
  137. checkrcs(dir, p)
  138.     char *dir;
  139.     char *p;
  140. {
  141.     DIR *df;
  142.     struct dirent *dp;
  143.     struct stat fs;
  144.     int i;
  145.  
  146.     if (!(df = opendir(dir))) {
  147.     fprintf(stderr, "cannot open: %s\n", dir);
  148.     return;
  149.     }
  150.     while (dp = readdir(df)) {
  151.     i = strlen(dp->d_name);
  152.     if (dp->d_name[i - 1] == 'v' && dp->d_name[i - 2] == ',') {
  153.         strcpy(p, dp->d_name);
  154.         p[i - 2] = '\0';
  155.         if (Stat(dir, &fs) < 0) {
  156.         strcpy(p, "RCS/");
  157.         strcat(p, dp->d_name);
  158.         printf("not used: %s\n", dir);
  159.         }
  160.     }
  161.     }
  162.     closedir(df);
  163. }
  164.  
  165. int
  166. Strncmp(cp1, cp2, n)
  167.     char *cp1, *cp2;
  168.     int n;
  169. {
  170.     char c1, c2;
  171.  
  172.     for (; --n >= 0 && *cp1 && *cp2; cp1++, cp2++) {
  173.     if (*cp1 != *cp2) {
  174.         c1 = *cp1;
  175.         c2 = *cp2;
  176.         if (c1 >= 'A' && c1 <= 'Z')
  177.         c1 += 'a' - 'A';
  178.         else if (c1 == '-')
  179.         c1 = '_';
  180.         if (c2 >= 'A' && c2 <= 'Z')
  181.         c2 += 'a' - 'A';
  182.         else if (c2 == '-')
  183.         c2 = '_';
  184.         if (c1 != c2)
  185.         return (int)c1 - (int)c2;
  186.     }
  187.     }
  188.     if (n < 0)
  189.     return 0;
  190.     return (int)*cp1 - (int)*cp2;
  191. }
  192.  
  193. int
  194. fncomp(n1, n2)
  195.     char **n1, **n2;
  196. {
  197.     int i, res;
  198.     char *cp1, *cp2;
  199.     char c1, c2;
  200.  
  201.     i = Strncmp(*n1, *n2, 8);
  202.     if (!i) {
  203.     cp1 = strrchr(*n1, '.');
  204.     cp2 = strrchr(*n2, '.');
  205.     if (cp1 || cp2) {
  206.         if (!cp1)
  207.         return -1;
  208.         if (!cp2)
  209.         return 1;
  210.         i = Strncmp(cp1 + 1, cp2 + 1, 3);
  211.     }
  212.     }
  213.     return i;
  214. }
  215.  
  216. void
  217. checkdir(dir)
  218.     char *dir;
  219. {
  220.     DIR *df;
  221.     struct dirent *dp;
  222.     char *p;
  223.     struct stat fs;
  224.     char *s, **names;
  225.     int i, max;
  226.  
  227.     if (!(df = opendir(dir))) {
  228.     fprintf(stderr, "cannot open: %s\n", dir);
  229.     return;
  230.     }
  231.     p = dir + strlen(dir);
  232.     if (p[-1] != '/')
  233.     *p++ = '/';
  234.     i = 0;
  235.     max = 0;
  236.     names = NULL;
  237.     while (dp = readdir(df)) {
  238.     strcpy(p, dp->d_name);
  239.     if (Stat(dir, &fs) < 0) {
  240.         perror(dir);
  241.         continue;
  242.     }
  243.     if ((fs.st_mode & S_IFDIR) == S_IFDIR) {
  244.         if (dp->d_name[0] == '.' &&
  245.         (dp->d_name[1] == '\0' || (dp->d_name[1] == '.' &&
  246.                        dp->d_name[2] == '\0')))
  247.         continue;
  248.         if (!strcmp (dp->d_name, "RCS")) {
  249.         if (dorcs)
  250.             checkrcs(dir, p);
  251.         continue;
  252.         }
  253.         if (!strcmp (dp->d_name, "SCCS"))
  254.         continue;
  255.         if (!strcmp (dp->d_name, "CVS.adm"))
  256.         continue;
  257.         checkfile(dir, p, &fs);
  258.         checkdir(dir);
  259.         continue;
  260.     }
  261.     checkfile(dir, p, &fs);
  262.     if (dorcs && !dontcare(dp->d_name)) {
  263.         strcpy(p, "RCS/");
  264.         strcat(p, dp->d_name);
  265.         strcat(p, ",v");
  266.         if (Stat(dir, &fs) < 0) {
  267.         strcpy(p, dp->d_name);
  268.         printf("no RCS: %s\n", dir);
  269.         }
  270.     }
  271.     if (do83) {
  272.         s = (char *)malloc(strlen(dp->d_name) + 1);
  273.         strcpy(s, dp->d_name);
  274.         if (i >= max) {
  275.         max += 25;
  276.         if (names)
  277.             names = (char **)realloc((char *)names,
  278.                          (max + 1) * sizeof(char *));
  279.         else
  280.             names = (char **)malloc((max + 1) * sizeof(char *));
  281.         }
  282.         names[i++] = s;
  283.     }
  284.     }
  285.     closedir(df);
  286.     if (do83) {
  287.     qsort((char *)names, i, sizeof(char *), fncomp);
  288.     max = i - 1;
  289.     *p = '\0';
  290.     for (i = 0; i < max; i++) {
  291.         if (!fncomp(&names[i], &names[i + 1]))
  292.         printf("8+3 clash: %s%s and %s\n",
  293.                dir, names[i], names[i + 1]);
  294.         free(names[i]);
  295.     }
  296.     if (names) {
  297.         free(names[i]);
  298.         free((char *)names);
  299.     }
  300.     }
  301. }
  302.  
  303. main(argc, argv)
  304.     int argc;
  305.     char **argv;
  306. {
  307.     char buf[2048];
  308.  
  309.     argc--;
  310.     argv++;
  311.     while (argc > 0) {
  312.     if (!strcmp(*argv, "-rcs")) {
  313.         dorcs = 0;
  314.         argc--;
  315.         argv++;
  316.     } else if (!strcmp(*argv, "-83")) {
  317.         do83 = 0;
  318.         argc--;
  319.         argv++;
  320.     } else if (!strcmp(*argv, "-ro")) {
  321.         doro = 0;
  322.         argc--;
  323.         argv++;
  324.     } else if (!strcmp(*argv, "-dot")) {
  325.         dodot = 0;
  326.         argc--;
  327.         argv++;
  328.     } else if (!strcmp(*argv, "-twiddle")) {
  329.         dotwiddle = 0;
  330.         argc--;
  331.         argv++;
  332.     } else
  333.         break;
  334.     }
  335.     if (!argc) {
  336.     strcpy(buf, ".");
  337.     checkdir(buf);
  338.     } else
  339.     while (--argc >= 0) {
  340.         strcpy(buf, *argv++);
  341.         checkdir(buf);
  342.     }
  343. }
  344.